package com.ekuaitu.kuaitu.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.adapter.CarListAdapter;
import com.ekuaitu.kuaitu.api.ApiStore;
import com.ekuaitu.kuaitu.api.UrlConstant;
import com.ekuaitu.kuaitu.base.BaseActivity;
import com.ekuaitu.kuaitu.bean.CarListBean;
import com.ekuaitu.kuaitu.utils.AppManager;
import com.ekuaitu.kuaitu.utils.Constants;
import com.ekuaitu.kuaitu.utils.IntentKey;
import com.ekuaitu.kuaitu.utils.MyApplication;
import com.ekuaitu.kuaitu.utils.StatusBarUtil;
import com.ekuaitu.kuaitu.utils.Toast.ToastCompat;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CarListActivity extends BaseActivity {

    @BindView(R.id.iv_return_list)
    ImageView ivReturnList;
    @BindView(R.id.expandableListView_carList)
    ExpandableListView expandableListViewCarList;
    @BindView(R.id.lin_empty)
    AutoRelativeLayout lin_empty;
    @BindView(R.id.progressBar_car_list)
    AutoRelativeLayout progressBar_car_list;

    private Context mContext = this;
    private CarListAdapter adapter;
    private List<Map<String, String>> list_group;
    private Map<String, List<Map<String, String>>> map_child;

    @Override
    public int getLayoutId() {
        return R.layout.activity_car_list;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimaryWhite), 0,true);
    }

    @Override
    public void initData() {
        super.initData();
        Intent intent = getIntent();
        String latitude = intent.getStringExtra(IntentKey.INTENT_LATITUDE);
        String longitude = intent.getStringExtra(IntentKey.INTENT_LONGITUDE);
        loadCarListData(latitude, longitude);
        //返回按钮监听
        ivReturnList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    //获取车辆列表
    private void loadCarListData(String latitude, String longitude) {
        progressBar_car_list.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getCarList(MyApplication.getInstance().getRequestFieldMap(),longitude, latitude).enqueue(new Callback<CarListBean>() {
            @Override
            public void onResponse(Call<CarListBean> call, Response<CarListBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        List<CarListBean.AttachmentBean.ParksBean> result = response.body().getAttachment().getParks();
                        if (result.size() != 0) {
                            list_group = new ArrayList<Map<String, String>>();
                            map_child = new HashMap<String, List<Map<String, String>>>();
                            for (int i = 0; i < result.size(); i++) {
                                Map groupMap = new HashMap<String, String>();
                                groupMap.put("groupName", "group" + i);
                                groupMap.put("locationName", result.get(i).getLocationName());
                                groupMap.put("areaName", result.get(i).getAreaName());
                                groupMap.put("distance", (result.get(i).getDistance() / 10) / 100F + "" + "公里");
                                list_group.add(groupMap);
                                List<Map<String, String>> listChild = new ArrayList<Map<String, String>>();
                                for (int j = 0; j < result.get(i).getCarModelList().size(); j++) {
                                    Map childMap = new HashMap<String, String>();
                                    childMap.put("carId", result.get(i).getCarModelList().get(j).getId());
                                    childMap.put("parkId", result.get(i).getCarModelList().get(j).getParkId());
                                    childMap.put("carPicture", result.get(i).getCarModelList().get(j).getPicture());
                                    childMap.put("carLatitude", result.get(i).getCarModelList().get(j).getLatitude());
                                    childMap.put("carLongitude", result.get(i).getCarModelList().get(j).getLongitude());
                                    childMap.put("carBrand", result.get(i).getCarModelList().get(j).getBrand());
                                    childMap.put("seats", result.get(i).getCarModelList().get(j).getSeat());
                                    childMap.put("carModel", result.get(i).getCarModelList().get(j).getModel());
                                    childMap.put("parkName", result.get(i).getCarModelList().get(j).getParkName());
                                    childMap.put("carLicense", result.get(i).getCarModelList().get(j).getLicenseTag());
                                    childMap.put("carDuration", result.get(i).getCarModelList().get(j).getMileage());
                                    childMap.put("dayPrice", result.get(i).getCarModelList().get(j).getValuationRuleModel().getWeekDayPrice());
                                    childMap.put("nightPrice", result.get(i).getCarModelList().get(j).getValuationRuleModel().getWeekNightPrice());
                                    childMap.put("passengerRisk", result.get(i).getCarModelList().get(j).getCompanyByRisk());
                                    childMap.put("sdew", result.get(i).getCarModelList().get(j).getAbatement());
                                    listChild.add(childMap);
                                }
                                map_child.put("group" + i, listChild);
                            }
                            initListView();
                            progressBar_car_list.setVisibility(View.GONE);
                        } else {
                            progressBar_car_list.setVisibility(View.GONE);
                            lin_empty.setVisibility(View.VISIBLE);
                        }
                    } else {
                        progressBar_car_list.setVisibility(View.GONE);
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<CarListBean> call, Throwable t) {
                progressBar_car_list.setVisibility(View.GONE);
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }

        });
    }

    private void initListView() {
        expandableListViewCarList = (ExpandableListView) findViewById(R.id.expandableListView_carList);
        adapter = new CarListAdapter(getApplicationContext(), list_group, map_child);
        expandableListViewCarList.setAdapter(adapter);
        //让Group都显示为展开状态
        for (int i = 0; i < list_group.size(); i++) {
            expandableListViewCarList.expandGroup(i);
        }
        expandableListViewCarList.setGroupIndicator(null);
        expandableListViewCarList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Map childMap = (Map) adapter.getChild(groupPosition, childPosition);
                Map groupMap = (Map) adapter.getGroup(groupPosition);
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                //从列表的item获得数据，适配为ParkDetailBean。以后改为传递模型类。
                bundle.putString("carBrand", childMap.get("carBrand") + "");
                bundle.putString("carModel", childMap.get("carModel") + "");
                bundle.putString("carLicense", childMap.get("carLicense") + "");
                bundle.putInt("carDuration", Integer.parseInt(childMap.get("carDuration") + ""));
                bundle.putString("carId", childMap.get("carId") + "");
                bundle.putString("parkId", childMap.get("parkId") + "");
                bundle.putString("picture", childMap.get("carPicture") + "");
                bundle.putDouble("longitude", Double.parseDouble(childMap.get("carLongitude") + ""));
                bundle.putDouble("latitude", Double.parseDouble(childMap.get("carLatitude") + ""));
                bundle.putString("areaName", groupMap.get("areaName") + "");
                bundle.putString("parkName", childMap.get("parkName") + "");
                bundle.putString("locationName", groupMap.get("locationName") + "");
                bundle.putInt("seats", Integer.parseInt(childMap.get("seats") + ""));
                String seats = childMap.get("seats") + "";
                bundle.putString("dayPrice", childMap.get("dayPrice") + "");
                bundle.putString("nightPrice", childMap.get("nightPrice") + "");
                bundle.putDouble("passengerRisk", Double.parseDouble(childMap.get("passengerRisk") + ""));
                bundle.putDouble("sdew", Double.parseDouble(childMap.get("sdew") + ""));
                intent.putExtras(bundle);
                setResult(Constants.RESULT_CARLIST, intent);
                finish();
                return false;
            }
        });
    }

}